package pruebas;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import junit.framework.TestCase;
import negModel.AlgoPesos;
import negModel.Llanta;
import negModelExcep.*;

public class LlantaTest extends TestCase {
	
	Llanta llanta1,llanta2;
	Element e;
	
	public void setUp() throws Exception {
		super.setUp();
		llanta1 = new Llanta (100, 25,165,new String ("llanta1"),new AlgoPesos(100),false,10,0.05);

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
	    
	    e = doc.createElement("parte");
		e.setAttribute("Clase","aleron");
		
		e.setAttribute("maxVU", ""+ llanta1.getMaxVU());
		e.setAttribute("vidaUtil", ""+ llanta1.getVidaUtil());
		e.setAttribute("peso", ""+ llanta1.getPeso());
		e.setAttribute("aportevmax", ""+ llanta1.aportevmax());
		e.setAttribute("detalle", llanta1.getDetalle());
		e.setAttribute("precio", ""+(llanta1.getPrecio()).getMonto());
		e.setAttribute("removible", ""+llanta1.esRemovible());
		e.setAttribute("performance", ""+llanta1.getPerformance());
		
		e.setAttribute("perimetro", ""+llanta1.getPerimetro());
		
	}
	
	public void testGastar() {
		try {
			llanta1.gastar();
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
		assertEquals (99,llanta1.getVidaUtil(),1);
		
		
	}

	public void testFuncionar() {
		try {
			assertEquals (0.05,llanta1.funcionar(),0.1);
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
			
	}

	public void testGetPerimetro() {
		assertEquals (0.05,llanta1.getPerimetro(),0.1);
	}

	public void testSetPerformance() {
		llanta1.setPerformance(20);
		assertEquals (20,llanta1.getPerformance(),0.1);
	}

	public void testSetMaxVU() {
		llanta1.setMaxVU(100);
		assertEquals (100,llanta1.getMaxVU(),1);
	}

	public void testGetVidaUtil() {
		llanta1.setMaxVU(100);
		llanta1.arreglar();
		assertEquals(100,llanta1.getVidaUtil(),1); // por aca
	}
	
	public void testToXml () throws Exception {
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
		
		e=llanta1.toXml(doc);
		
		assertEquals(llanta1.getPerimetro(),Double.parseDouble(e.getAttribute("perimetro")),0.0);
		
		assertEquals(llanta1.getPerformance(),Integer.parseInt(e.getAttribute("performance")));
		assertEquals((llanta1.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(llanta1.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(llanta1.getDetalle(),e.getAttribute("detalle"));
		assertEquals(llanta1.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(llanta1.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
		
	}
	
	public void testLlanta()throws Exception {
		
		llanta2=new Llanta(e);
				
		assertEquals(llanta2.getPerimetro(),Double.parseDouble(e.getAttribute("perimetro")),0.0);
		
		assertEquals(llanta2.getPerformance(),Integer.parseInt(e.getAttribute("performance")));
		assertEquals((llanta2.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(llanta2.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(llanta2.getDetalle(),e.getAttribute("detalle"));
		assertEquals(llanta2.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(llanta2.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
		
	}

	
}
